package com.zero.rabbitmq.routing;

import com.rabbitmq.client.*;
import com.zero.rabbitmq.utils.ConnectionUtil;
import com.zero.rabbitmq.utils.RabbitConstant;

import java.io.IOException;

/**
 * 消费者
 */
public class Baidu {

    public static void main(String[] args) throws Exception {

        Connection connection = ConnectionUtil.getConnection();

        final Channel channel = connection.createChannel();


        /**
         * Params:
         * queue - queue name
         * durable - 是否声明一个持久队列
         * exclusive –  是否声明了一个排他队列
         * autoDelete – 是否声明一个自动删除队列
         * arguments – 参数
         */
        //声明队列信息
        channel.queueDeclare(RabbitConstant.QUEUE_BAIDU,false,false,false,null);

        /**
         * Params:
         * queue – the name of the queue
         * exchange – the name of the exchange
         * routingKey – the routing key to use for the binding
         * arguments – other properties (binding parameters)
         */
        //queueBind用于将队列与交换机绑定
        channel.queueBind(RabbitConstant.QUEUE_BAIDU,RabbitConstant.EXCHANGE_WEATHER_ROUTING,"china.hunan.changsha.20201127");
        channel.queueBind(RabbitConstant.QUEUE_BAIDU,RabbitConstant.EXCHANGE_WEATHER_ROUTING,"china.hubei.wuhan.20201127");
        channel.basicQos(1);
        channel.basicConsume(RabbitConstant.QUEUE_BAIDU , false , new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException, IOException {
                System.out.println("百度天气收到气象信息：" + new String(body));
                channel.basicAck(envelope.getDeliveryTag() , false);
            }
        });

    }
}
